package com.gill.touka.controller;

import com.gill.touka.domain.entity.User;
import com.gill.touka.util.JwtOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private JwtOperator jwtOperator;

    /**
     * 测试：获取指定服务名的所有实例数据
     */
    @GetMapping("/nacos")
    public List<ServiceInstance> test1(){
        return discoveryClient.getInstances("user-center");
    }

    /**
     * 测试：feign如何构造"多参数"的get请求
     * 案例：?id=1&wxId=2
     */
    @GetMapping("/feign/get")
    public User test2(User user){
        return user;
    }

    /**
     * 测试：feign如何构造"多参数"的post请求
     * 案例：{id=1,wxId=2}
     */
    @PostMapping("/feign/post")
    public User test3(@RequestBody User user){
        return user;
    }

    /**
     * 模拟生成token
     *
     * @return token
     */
    @GetMapping("/gen-token")
    public String genToken() {
        Map<String, Object> userInfo = new HashMap<>();
        userInfo.put("id", 1);
        userInfo.put("userName", "gill");
        userInfo.put("role", "user");
        return jwtOperator.generateToken(userInfo);
    }
}
